home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Games / MacGnuGo 0.5e / gnugo.src / macmain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-17  |  5.0 KB  |  208 lines  |  [TEXT/R*ch]

  1. /*
  2.                 GNU GO - the game of Go (Wei-Chi)
  3.                 Version 1.1   last revised 3-1-89
  4.            Copyright (C) Free Software Foundation, Inc.
  5.                       written by Man L. Li
  6.                       modified by Wayne Iba
  7.                     documented by Bob Webber
  8.         mac port by Ron Nicholson - 93Oct
  9. */
  10. /*
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation - version 1.
  14.  
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. GNU General Public License in file COPYING for more details.
  19.  
  20. You should have received a copy of the GNU General Public License
  21. along with this program; if not, write to the Free Software
  22. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. */
  25.  
  26. #define EMPTY 0
  27. #define WHITE 1
  28. #define BLACK 2
  29.  
  30. unsigned char p[19][19], l[19][19], ma[19][19], ml[19][19];
  31. int mymove, umove;
  32. int rd, lib, play, pass;
  33. int mik, mjk, uik, ujk, mk, uk;  /* piece captured */
  34. int opn[9];  /* opening pattern flag */
  35.  
  36. /* added from NextGo */
  37. int MAXX, MAXY, blackCapturedKoI, blackCapturedKoJ, whiteCapturedKoI,
  38.   whiteCapturedKoJ, blackCaptured, whiteCaptured,
  39.   blackStones, whiteStones, currentStone, opposingStone, blackPassed, whitePassed;
  40. int blackTerritory, whiteTerritory;
  41.  
  42. unsigned char capturedmat[19][19];
  43. int mx, my;
  44. int hcap;
  45. int uflag;
  46. int size = 12;
  47.  
  48. initstuff()
  49. {
  50.    int i, j;
  51.  
  52. /* init opening pattern numbers to search */
  53.    for (i = 0; i < 9; i++)
  54.         opn[i] = 1;
  55.    opn[4] = 0;
  56.  
  57. /* init board */
  58.    for (i = 0; i < 19; i++)
  59.      for (j = 0; j < 19; j++) { p[i][j] = EMPTY; capturedmat[i][j] = 0; }
  60.           
  61. /* init global variables */
  62.    mk = 0;  uk = 0;
  63.    blackCaptured = 0, whiteCaptured = 0, 
  64.    blackTerritory = 0; whiteTerritory = 0;
  65.    blackStones = whiteStones = 0;
  66.  
  67.    play = 1;
  68.    pass = 0;
  69.    mik = -1; mjk = -1;
  70.    uik = -1; ujk = -1;
  71.    seed(&rd);    /* start random number seed */
  72.    mx = my = -1;
  73. }
  74.  
  75. setcolor(int color)
  76. {
  77.       if (color != WHITE) {
  78.          mymove = WHITE;   /* computer white */
  79.          umove = BLACK;   /* human black */
  80.       } else {
  81.          mymove = BLACK;   /* computer black */
  82.          umove = WHITE;   /* human white */
  83.       }
  84.       currentStone = mymove; opposingStone = umove; 
  85. }
  86.  
  87. init4(side)
  88. int side;
  89. {
  90.    int i, j;
  91.    char move[10], ans[5];
  92.    
  93.    if (MAXX < 5) MAXY = MAXX = 5;
  94.    if (MAXX > 19) MAXY = MAXX = 19;
  95.    if (hcap < 1) hcap = 0;
  96.    if (hcap > MAXX) hcap = MAXX;
  97.    
  98.    initstuff();
  99.    sethand(hcap);
  100.    setcolor(side);
  101.    initHistory();
  102.   
  103.    if (uflag)  /* start game */
  104.      {
  105.      if (side == 2) /* black */
  106.         {
  107.          if (hcap != 0)
  108.        {
  109.             genmove(&i, &j);   /* computer move */
  110.             mx = i; my = j;
  111.             p[i][j] = mymove;
  112.             addHistory(WHITE,i,j);
  113.           }
  114.        }
  115.       else
  116.         {
  117.         if (hcap == 0)
  118.        {
  119.             genmove(&i, &j);   /* computer move */
  120.             mx = i; my = j;
  121.             p[i][j] = mymove;
  122.             addHistory(BLACK,i,j);
  123.           }
  124.        }
  125.     }
  126. }
  127.  
  128. score_game_c()
  129. {
  130.   int i,j;
  131.   score_game();
  132.   blackStones = whiteStones = 0;
  133.   for (i=0;i<MAXX;i++) for (j=0;j<MAXY;j++) {
  134.     if (p[i][j] == BLACK) blackStones++;
  135.     if (p[i][j] == WHITE) whiteStones++;
  136.   }
  137. }  
  138.  
  139. movenplay(x,y)
  140. int x,y;
  141. {
  142.   int i, j;
  143.   i = x; j = y;
  144.   if (i >= 0)   /* not pass */ {
  145.     p[i][j] = umove; pass = 0;
  146.     if (examboard(mymove)) RedrawScreen();     /* remove my dead pieces */
  147.   } else pass++;
  148.   addHistory(opposingStone,i,j);
  149.   if (pass == 2)
  150.     score_game_c();
  151.   else if (uflag == 0) {
  152.     if (umove == WHITE)  setcolor(BLACK);
  153.     else setcolor(WHITE);
  154.     mx = i; my = j;
  155.   } else {
  156.     genmove(&i, &j);   /* computer move */
  157.     mx = i; my = j;
  158.     if (i >= 0)   /* not pass */ {
  159.       p[i][j] = mymove; pass = 0;
  160.     } else pass++;
  161.     examboard(umove);   /* remove your dead pieces */
  162.     addHistory(currentStone,i,j);
  163.   }
  164.   if (pass == 2) score_game_c();
  165. }
  166.  
  167. on_new() { init4(uflag); resize(); }
  168.  
  169. do_pass() { if (pass < 2) movenplay(-1,-1); RedrawScreen(); }
  170.  
  171. on_black() { uflag = BLACK; setcolor(uflag); refresh(); }
  172. on_white() { uflag = WHITE; setcolor(uflag); refresh(); }
  173. on_both()  { uflag = 0; }
  174. on_bigger()  { MAXX = MAXY += 1+(MAXX > 8); /* */ on_new(); }
  175. on_smaller() { MAXX = MAXY -= 1+(MAXX > 9); /* */ on_new(); }
  176. on_more() { hcap++; on_new(); }
  177. on_less() { hcap--; on_new(); }
  178. void on_undo() { if (uflag) undo(2); else undo(1); refresh(); }
  179. on_randomize(int flag) { return; }
  180.  
  181. extern char *AboutMenu;
  182. extern char *AboutText;
  183.  
  184. main()
  185. {
  186.   MAXX = MAXY = 9;
  187.   uflag = 1; hcap = 2;
  188.   
  189.   AboutMenu = "About GnuGo...;-";
  190.   macinit("GnuGo", 2*size*(MAXX+2) + 120, 2*size*(MAXX+2));
  191.   AboutText = "GnuGo V0.5e 93Nov17 ©1993 FSF";
  192.   
  193.   init4(uflag);
  194.   while(1) {
  195.     DoEvent();
  196.   }
  197. }
  198.  
  199. /*
  200.  * current bugs:
  201.  *   flicker on refresh
  202.  * added:
  203.  *   mark latest move, fiot, undo, show scored board
  204.  *   play both sides, play 5x5, undo game over, chinese scoring, captured keepout
  205.  * fixed:
  206.  *   crash on zero handicap, suicide moves
  207.  */
  208.